<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Examinfo;
use App\Models\Student;
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class ExaminfoController extends CommonController
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //withTrashed 包换软删除
        $data=Examinfo::orderBy('id','desc')->paginate($this->page);
        return view('admin.examinfo.index',compact('data'));
    }


    public function create()
    {
        return view('admin.examinfo.create');
    }

    public function store(Request $request)
    {
      $post=$this->validate($request,[
          'test_name'=>'required',
          'class_name'=>'required',
          'teacher'=>'required',
          'class_teacher'=>'required',
          'full_mark'=>'required',
          'subject'=>'required',
      ]);
      Examinfo::create($post);
        return redirect(route('admin.examinfo.index'))->with(['error'=>'添加成功']);
    }

    public function show(Examinfo $examinfo)
    {

    }


    public function edit(Examinfo $examinfo)
    {
        return view('admin.examinfo.edit',compact('examinfo'));
    }

    public function update(Request $request, Examinfo $examinfo)
    {
        $post=$this->validate($request,[
            'test_name'=>'required',
            'class_name'=>'required',
            'teacher'=>'required',
            'class_teacher'=>'required',
            'full_mark'=>'required',
            'subject'=>'required',
        ]);
        $examinfo->update($post);
        return redirect(route('admin.examinfo.index'))->with(['error'=>'修改成功']);
    }

    public function destroy(Examinfo $examinfo)
    {
        $examinfo->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
    //导出考试信息
    public function examination(Request $request){
        $data=Examinfo::orderBy('id','desc')
            ->where([
                ['test_name',$request->input('test_name')],
                ['class_name',$request->input('class_name')]
            ])
            ->paginate($this->page);
        if ($request->isMethod('post')){
            $post=$this->validate($request,[
               'test_name' =>'required',
                'class_name'=>'required',
            ]);
            //获取科目
            $subject=Examinfo::where([
                ['test_name',$request->input('test_name')],
                ['class_name',$request->input('class_name')]
                ])->pluck('subject')->toArray();
           //获取学生
            $data=Student::where('class_name',$request->input('class_name'))->pluck('name','student_code')->toArray();

            $title = ['姓名', '学号'];
            // Create new Spreadsheet object
            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // 方法一，使用 setCellValueByColumnAndRow
            //表头
            //设置单元格内容
            foreach ($title as $key => $value) {
                // 单元格内容写入
                $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
            }
            foreach ($subject as $key => $value) {
                // 单元格内容写入
                $sheet->setCellValueByColumnAndRow($key + 3, 1, $value);
            }
            //第一个参数 单元格的数字列坐标
            //第二给参数  单元格的数字行坐标
            //第三个参数 值
            $x=1;
            foreach ($data as $key=>$val){
                //第二行开始插入
                $x++;
                //姓名
                $sheet->setCellValueByColumnAndRow(1,$x , $val);
                //学号
                $sheet->setCellValueByColumnAndRow(2,$x , $key);
            }
            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
            // Redirect output to a client’s web browser (Xlsx)
             header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
              header('Content-Disposition: attachment;filename="'.$request->input('class_name').$request->input('test_name').'.xlsx"');
              header('Cache-Control: max-age=0');
              // If you're serving to IE 9, then the following may be needed
              header('Cache-Control: max-age=1');
              // If you're serving to IE over SSL, then the following may be needed
              header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
              header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
              header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
              header('Pragma: public'); // HTTP/1.0*/
            //  $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
             $writer->save('php://output');
            exit;
        }
        return view('admin.examinfo.examination',compact('data'));
    }
}
